<?php
namespace app\common\controller;
use app\plugin\controller\Excel;
use think\Db;

Class Table extends Curd {
    protected $data = null;
    protected $where= null;
    protected $count = null;
    protected $actionData = null;
    protected $excelData =null;

    protected function _initialize(){
        parent::_initialize();
        $this->is_pri('read');
    }

    /**
     * 生成excel导出链接
     * @params
     * type $string 分类 common||recycle
     * title $string 表格标题,
     * filename $string 表格的文件命名及文件后缀 .xls|.xlsx
     * count $int 该数据组的总数
     * size $int 每张表要导出的数据个数
     * where $arr|string 数据的条件
     * @return string|array
     */
    protected function excelList($type='common',$title,$filename,$count,$size=null,$where=[]){
        $excel = [];
        if(!empty($where)){
            foreach ($where as $k=>$v){
                $key1 = $k;
            }
            if($key1!='create_time'&&$key1!='update_time'){
                $search  = $where[$key1];
                $true_search = $search[count($search)-1];
                $true_search = explode('%',$true_search);
                $where[$key1][count($search)-1] = $true_search[1];
            }
        }
        $where = encode($where);
        $times = ceil($count/$size);
        for ($i=0;$i<$times;$i++){
            $index = intval($i+1);
            $url = "excelUrl?type={$type}&page={$index}&size={$size}&where={$where}&filename={$filename}({$index}).xls&title={$title}({$index})";
            array_push($excel,['url'=>$url,'filename'=>$filename .'('. $index .')']);
        }
        return $excel;
    }

    /**
     * 导出表单的数据链接处理环节
     * @params
     * key $string 表单键值
     * return_data 是否要返回数据（不返回就直接生成表单，返回则进一步处理完数据再自行生成）
     * @return string|array
     */
    public function excelUrl($key=[],$return_data=0){
        $params = $this->params;
        if(!isset($params['type'])||!isset($params['page'])||!isset($params['size'])||!isset($params['filename'])
        ||!isset($params['title'])||!isset($params['where'])){
            exit('参数缺失！');
        }
        else{
            $where = decode( $params['where'] );
            $key1 = -1;
            foreach ($where as $k=>$v){
                $key1 = $k;
            }
            if($key1!=-1){
                $search  = $where[$key1];
                $true_search = $search[count($search)-1];
                $true_search = "%$true_search%";
                $where[$key1][count($search)-1] = $true_search;
            }

            if($params['type']=='common'){
                $res = Db::name($this->table)->
                     where('is_delete',0)
                    ->where($where)->field($this->field[0],$this->field[1])->
                    order('id')->
                    page($params['page'],$params['size'])->select();
                $res = [
                    'code' => 1,
                    'msg' => 'success',
                    'data' => $res,
                    'total' => $this->count($where,0),
                    'page' => $params['page'],
                    'size' => $params['size']
                ];
            }
            if($params['type']=='recycle'){
                $res = Db::name($this->table)->where('is_delete',1)->where($where)->
                field($this->field[0],$this->field[1])->order('id')
                    ->page($params['page'],$params['size'])->select();
                $res = [
                    'code' => 1,
                    'msg' => 'success',
                    'data' => $res,
                    'total' => $this->count($where,1),
                    'page' => $params['page'],
                    'size' => $params['size']
                ];
            }

            if($res['code']==1){
                if(empty($key)){
                    foreach ($res['data'][0] as $k=>$v){
                        array_push($key,$k);
                    }
                }
                $rows = ['title'=>$params['title'],'filename'=>$params['filename'],'data'=>$res['data'],'key'=>$key];
                if($return_data){
                    $this->excelData = $rows;
                }
                else{
                    $excel = new Excel();
                    $excel->index($rows);
                }
            }
            else{
                exit('数据获取失败！');
            }
        }
    }

    protected function data($page = 1,$limit = 15){
        $this->is_pri('read');
        $params = $this->params;
        $where = [];$this->count = 0;
        if(isset($params['page'])&&isset($params['limit'])){
            $page = $params['page'];$limit = $params['limit'];
        }
        if($limit>1000){
            $res = ['code'=>1,'msg'=>'请求数据数量过多！'];
        }
        else{
            if(isset($params['time_key'])
                &&isset($params['time_val'])
                &&$params['time_key']
                &&$params['time_val']
            ){
                $time_key = $params['time_key'];
                $time_val = $params['time_val'];
                $time_val = explode(' 至 ',$time_val);
                $where[$time_key] =['between',$time_val];
            }

            if(isset($params['search_key'])
                &&isset($params['search_val'])
                &&$params['search_key']
                &&$params['search_val']
            ){
                $search_key = $params['search_key'];
                $search_val = $params['search_val'];
                $where[$search_key] = ["like","%$search_val%"];
            }
            $res = obj_decode( $this->page($page,$limit,$where,'sort desc,id desc') );
            if($res['code']){
                $res['code']=0;
                $res['count'] = $res['total'];
                $this->count = $res['total'];
            }
            else{
                $res['code']=1;
            }
        }
        $this->where = $where;
        $this->data = $res;
    }
    protected function index(){
        $this->is_pri('read',0);
        $params = $this->params;
        $time_key = '';$time_val = '';$search_key = '';$search_val = '';
        if(isset($params['time_key'])){
            $time_key = $params['time_key'];
        }
        if(isset($params['time_val'])){
            $time_val = $params['time_val'];
        }
        if(isset($params['search_key'])){
            $search_key = $params['search_key'];
        }
        if(isset($params['search_val'])){
            $search_val = $params['search_val'];
        }

        $this->assign([
            'search'=>[
                'time_key'=>$time_key,
                'time_val'=>$time_val,
                'search_key'=>$search_key,
                'search_val'=>$search_val
            ]
        ]);
    }
    protected function detail(){
        $this->is_pri('read',0);
        $params = $this->params;
        if(!isset($params['id'])||!$params['id']){
            exit('参数缺失！');
        }
    }
    public function edit(){
        $this->is_pri('update',0);
        $params = $this->params;
        if(!isset($params['id'])||!$params['id']){
            exit('参数缺失！');
        }
        $data = obj_decode( $this->read($params['id']) );
        $this->data = $data['data'];
        $this->assign([
            'data'=>$data['data'],
            'id' => $params['id']
        ]);
        return $this->fetch();
    }

    public function add(){
        $this->is_pri('create',0);
        return $this->fetch();
    }

    protected function recycleData($page = 1,$limit = 15){
        $this->is_pri('delete');
        $params = $this->params;
        $where = [];$this->count=0;
        if(isset($params['page'])&&isset($params['limit'])){
            $page = $params['page'];$limit = $params['limit'];
        }
        if($limit>1000){
            $res = ['code'=>1,'msg'=>'请求数据数量过多！'];
        }
        else{
            if(isset($params['time_key'])
                &&isset($params['time_val'])
                &&$params['time_key']
                &&$params['time_val']
            ){
                $time_key = $params['time_key'];
                $time_val = $params['time_val'];
                $time_val = explode(' 至 ',$time_val);
                $where[$time_key] =[$time_val];
            }
            if(isset($params['search_key'])
                &&isset($params['search_val'])
                &&$params['search_key']
                &&$params['search_val']
            ){
                $search_key = $params['search_key'];
                $search_val = $params['search_val'];
                $where[$search_key] = ["like","%$search_val%"];
            }

            $res = obj_decode( $this->recycle($page,$limit,$where) );
            if($res['code']){
                $res['code']=0;
                $res['count'] = $res['total'];
                $this->count = $res['total'];
            }
            else{
                $res['code']=1;
            }
        }
        $this->data = $res;
        $this->where = $where;
    }

    protected function recycleindex(){
        $this->is_pri('delete',0);$params = $this->params;
        $time_key = '';$time_val = '';$search_key = '';$search_val = '';
        if(isset($params['time_key'])){
            $time_key = $params['time_key'];
        }
        if(isset($params['time_val'])){
            $time_val = $params['time_val'];
        }
        if(isset($params['search_key'])){
            $search_key = $params['search_key'];
        }
        if(isset($params['search_val'])){
            $search_val = $params['search_val'];
        }
        $this->assign([
            'search'=>[
                'time_key'=>$time_key,
                'time_val'=>$time_val,
                'search_key'=>$search_key,
                'search_val'=>$search_val
            ],
            'pri'=>$this->privilege
        ]);
    }

    public function switchBtn(){
        $params = $this->params;
        $this->is_pri('update');
        $data = $this->update($params);
        return $data;
    }

    public function tableCreate($is_create=1)
    {
        $this->is_pri('create');
        $params = $this->params;
        if($is_create){
            $res = obj_decode( $this->create($params) );
            $res['jump_url'] = true;
            $res['jump'] = 'index';
            exit( encode($res));
        }
        else{
            $this->actionData =  $params;
        }
    }

    public function tableUpdate($is_update=1){
        $this->is_pri('update');
        $params = $this->params;
        if($is_update){
            $res = obj_decode( $this->update($params));
            $res['jump_url'] = true;
            $res['jump'] = 'index';
            exit( encode($res) );
        }
        else{
            $this->actionData = $params;
        }
    }

}

